package com.oasystem.web.view;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.oasystem.domain.ProfitDetail;

public class ProfitTableExcelView extends AbstractExcelView {

	@Override
	protected void buildExcelDocument(Map<String, Object> map,
			HSSFWorkbook xls, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HSSFSheet sheet = xls.createSheet("Sheet");
		HSSFRow row = sheet.createRow(0);
		row.createCell(0).setCellValue("序号");
		sheet.setColumnWidth(0, 5*256);
		row.createCell(1).setCellValue("会员卡号");
		sheet.setColumnWidth(1, 25*256);
		row.createCell(2).setCellValue("会员名称");
		sheet.setColumnWidth(2, 10*256);
		row.createCell(3).setCellValue("建卡时间");
		sheet.setColumnWidth(3, 20*256);
		row.createCell(4).setCellValue("身份证");
		sheet.setColumnWidth(4, 25*256);
		row.createCell(5).setCellValue("联系电话");
		sheet.setColumnWidth(5, 20*256);
		row.createCell(6).setCellValue("操作员");
		sheet.setColumnWidth(6, 10*256);
		row.createCell(7).setCellValue("当天返利金额");
		sheet.setColumnWidth(7, 15*256);
		row.createCell(8).setCellValue("已反金额");
		sheet.setColumnWidth(8, 15*256);
		row.createCell(9).setCellValue("返利目标金额");
		sheet.setColumnWidth(9, 15*256);
		row.createCell(10).setCellValue("还剩金额");
		sheet.setColumnWidth(10, 15*256);
		List<ProfitDetail> list = (List<ProfitDetail>) map.get("profitDetails");
		int size = list.size();
		for (int i = 1; i <= size; i++) {
			HSSFRow row1 = sheet.createRow(i);
			ProfitDetail record = list.get(i-1);
			row1.createCell(0).setCellValue(i);
			row1.createCell(1).setCellValue(record.getMemberCardNumber());
			row1.createCell(2).setCellValue(record.getMemberName());
			row1.createCell(3).setCellValue(record.getCreateDate());
			row1.createCell(4).setCellValue(record.getIdCardNumber());
			row1.createCell(5).setCellValue(record.getPhoneNumber());
			row1.createCell(6).setCellValue(record.getOperatorName());
			row1.createCell(7).setCellValue(record.getTodayShouldReturnAmount());
			row1.createCell(8).setCellValue(record.getRemainAmount());
			row1.createCell(9).setCellValue(record.getTargetReturnAmount());
			row1.createCell(10).setCellValue(record.getRemainAmount());
		}
		String fileName = "profitReturned.xls";// 设置下载时客户端Excel的名称
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename="
				+ fileName);
		OutputStream ouputStream = response.getOutputStream();
		xls.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();
	}

}
